package com.briup.estore.web.servlet;


import com.briup.estore.common.result.Result;
import com.briup.estore.common.dto.OrderCreateDTO;
import com.briup.estore.common.utils.JsonUtils;
import com.briup.estore.common.vo.OrderDetailVO;
import com.briup.estore.service.OrderService;
import com.briup.estore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author briup-adam
 * @Date 2023/8/4 下午3:14
 * @Description 用户下单 创建订单并返回订单信息
 **/
@WebServlet("/order/create")
public class OrderCreateServlet extends  BaseServlet {
    private OrderService orderService=new OrderServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = readData(req);
        String userId = (String) req.getAttribute("userId");
        OrderCreateDTO dto = JsonUtils.toObject(data, OrderCreateDTO.class);
        OrderDetailVO order = orderService.createOrder(dto,Integer.parseInt(userId));
        writeData(resp, Result.success(order));
    }
}
